package com.weiaibenpao.demo.chislim.ui.activity.running;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.service.RunningService;
import com.weiaibenpao.demo.chislim.ui.widget.RunningDataView;
import com.weiaibenpao.demo.chislim.utils.Utils;


/**
 * Created by ZJL on 2017/12/19
 */

public class BaseOutdoorRunningActivity extends BaseRunningActivity {
    private final ContentObserver mGpsMonitor = new ContentObserver(null) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            final boolean isEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onGPSStateChanged(isEnabled);
                }
            });
        }
    };

    @Override
    public void doOnServiceConnectedFromRecovering() {
        super.doOnServiceConnectedFromRecovering();
        //如果在App被杀掉之后到重新打开之前期间用户关掉了GPS，那么恢复跑步时先暂停跑步
        if (!Utils.isGPSEnabled(this)) {
            if (mService.getRunningState() == RunningService.RUNNING) {
                pauseRunning();
                runningDataView.setState(RunningDataView.PAUSE);
            }
        }
    }

    protected void onGPSStateChanged(boolean isEnabled) {
        if(!isEnabled){
            if(mService!=null && mService.getRunningState() == RunningService.RUNNING ) {
                pauseRunning();
                runningDataView.setState(RunningDataView.PAUSE);
            }
        }
    }


    @Override
    public void onClickContinue() {//户外跑必须使用GPS，所以覆写BaseRunning里面的onClickContinue以加上GPS是否开启的判断
        if(goEnableGPS()){
            runningDataView.setState(RunningDataView.PAUSE);
            return;
        }
        continueRunning();
    }

    protected LocationManager mLocationManager;

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        checkPermission();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getContentResolver().registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), false, mGpsMonitor);
    }

    @Override
    protected void onDestroy() {
        getContentResolver().unregisterContentObserver(mGpsMonitor);
        super.onDestroy();
    }

    protected boolean goEnableGPS() {
        if (!Utils.isGPSEnabled(this)) {
            Utils.enableGPS(this);
            Toast.makeText(this, getString(R.string.gps_enable_notice), Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }


    private int REQUEST_FINE_LOCATION_CODE = 100;
    private boolean checkPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_FINE_LOCATION_CODE);//自定义的code
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (REQUEST_FINE_LOCATION_CODE == requestCode) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, getString(R.string.please_enable_gps), Toast.LENGTH_SHORT).show();
            } else {
                // 留给可能需要的处理逻辑
            }
        }
    }
}
